package com.byron.media.server.utils;

import com.byron.media.server.model.MediaData;

public class MediaType {

    // type 结构
    // 开始位(1) 结束位(1) 类型(4) 保留(2)

    public static int H264 = 0;
    public static int PCM = 1;
    public static int AAC = 2;
    public static int TEST = 3;         // 测试数据
    public static int PROXY = 4;         // 代理数据

    /**
     * 该数据包是否为开始包
     */
    public static boolean isStart(byte type){
        int start = (type >> 7) & 0x01;
        return start > 0;
    }

    /**
     * 该数据包是否为结束包
     */
    public static boolean isEnd(byte type){
        int end = (type >> 6) & 0x01;
        return end > 0;
    }

    /**
     * 该数据包是否H264
     */
    public static boolean isH264(byte type){
        int t = (type >> 2) & 0x0f;
        return t == H264;
    }

    /**
     * 该数据包是否为PCM
     */
    public static boolean isPCM(byte type){
        int t = (type >> 2) & 0x0f;
        return t == PCM;
    }

    /**
     * 是否是测试数据
     */
    public static boolean isTest(byte type){
        int t = (type >> 2) & 0x0f;
        return t == TEST;
    }

    public static boolean isAAC(byte type){
        int t = (type >> 2) & 0x0f;
        return t == AAC;
    }

    public static boolean isProxy(byte type){
        int t = (type >> 2) & 0x0f;
        return t == PROXY;
    }

    /**
     * 设置开始位
     */
    public static byte putStart(byte type){
        return (byte) (type | 0x80);
    }

    /**
     * 设置结束位
     */
    public static byte putEnd(byte type){
        return (byte) (type | 0x40);
    }

    /**
     * 设置H264类型
     */
    public static byte putH264(byte type){
        return (byte) ((type & 0xC3) | 0x00);
    }

    public static byte putPCM(byte type){
        return (byte) ((type & 0xC3) | 0x04);
    }

    public static byte putAAC(byte type){
        return (byte) ((type & 0xC3) | 0x08);
    }

    public static byte putTest(byte type){
        return (byte) ((type & 0xC3) | 0x0C);
    }

    public static byte putProxy(byte type){
        return (byte) ((type & 0xC3) | 0x10);
    }

    /**
     * 设置为来自无线终端
     */
    public static byte putFromWireless(byte type){
        return (byte) ((type & 0xFC) | 0x00);
    }

    /**
     * 设置为来自有线终端
     */
    public static byte putFromWire(byte type){
        return (byte) ((type & 0xFC) | 0x01);
    }

    /**
     * 是否来自无线终端
     */
    public static boolean isFromWireless(byte type){
        return (type & 0x03) == (byte)0x00;
    }

    /**
     * 是否来自有线终端
     */
    public static boolean isFromWire(byte type){
        return (type & 0x03) == (byte)0x01;
    }

    /**
     * 判断是否是I帧
     */
    public static boolean isIframe(MediaData data) {
        if(!isStart(data.getType())){
            return false;
        }
        if(!isH264(data.getType())){
            return false;
        }

        int startIndex = 0;
        for(int i = 0; i < data.getData().length; i++){
            if(data.getData()[i] == 0){
                continue;
            }
            startIndex = i;
            break;
        }

        if(startIndex > 4){
            return false;
        }

        byte naluType = data.getData()[startIndex + 1];
        naluType = (byte) (naluType & 0x1F);
        return naluType == 7;
//        return naluType == 5;
    }

}
